package com.jdd.modules.parkmonthly.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.AutoLog;
import com.jdd.modules.parkmonthly.dto.MonthlyDepartmentDto;
import com.jdd.modules.parkmonthly.entity.MonthlyDepartment;
import com.jdd.modules.parkmonthly.entity.MonthlyDepartmentTreeModel;
import com.jdd.modules.parkmonthly.service.IMonthlyDepartmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
 * @Description: 月卡车部门组织表
 * @Author: jdd
 * @Date:   2021-11-09
 * @Version: V1.0
 */
@Slf4j
@Api(tags="月卡车部门组织表")
@RestController
@RequestMapping("/monthlyDepartment")
public class MonthlyDepartmentController {

	@Resource
	private IMonthlyDepartmentService monthlyDepartmentService;

	/**
	 * 查询数据 查出所有部门,并以树结构数据格式响应给前端
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryTreeList", method = RequestMethod.POST)
	public Result<List<MonthlyDepartmentTreeModel>> queryTreeList() {
		Result<List<MonthlyDepartmentTreeModel>> result = new Result<>();
		try {
			List<MonthlyDepartmentTreeModel> list = monthlyDepartmentService.queryTreeList();
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	 @RequestMapping(value = "/addDepartment", method = RequestMethod.POST)
	 public Result<MonthlyDepartment> addFirstGroup(@RequestBody MonthlyDepartmentDto monthlyDepartmentDto){
		return monthlyDepartmentService.addDepartment(monthlyDepartmentDto);
	 }


	/**
	 *  编辑
	 * @param jsonObject
	 * @return
	 */
	@AutoLog(value = "月卡车部门组织表-根据id查询部门")
	@ApiOperation(value = "月卡车部门组织表-编辑", notes = "月卡车部门组织表-根据id查询部门")
	@PostMapping(value = "/queryById")
	public Result<MonthlyDepartment> queryById(@RequestBody JSONObject jsonObject) {
		String id = jsonObject.getString("id");
		Result<MonthlyDepartment> result = new Result<>();
		MonthlyDepartment monthlyDepartment = monthlyDepartmentService.getById(id);
		result.success("查询成功");
		result.setResult(monthlyDepartment);
		return result;
	}


	/**
	  *  编辑
	 * @param jsonObject
	 * @return
	 */
	@AutoLog(value = "月卡车部门组织表-编辑")
	@ApiOperation(value="月卡车部门组织表-编辑", notes="月卡车部门组织表-编辑")
	@PutMapping(value = "/edit")
	public Result<MonthlyDepartment> edit(@RequestBody JSONObject jsonObject) {
		String id=jsonObject.getString("id");
		String departName=jsonObject.getString("departName");
		Result<MonthlyDepartment> result = new Result<>();
		MonthlyDepartment monthlyDepartmentEntity = monthlyDepartmentService.getById(id);
		if(monthlyDepartmentEntity==null) {
			result.error500("未找到对应记录");
			return result;
		}else {
			//判断该车场该部门名称是否已存在
			MonthlyDepartmentDto monthlyDepartmentDto=new MonthlyDepartmentDto();
			monthlyDepartmentDto.setParkCode(monthlyDepartmentEntity.getParkCode());
			monthlyDepartmentDto.setDepartName(departName);
			if(monthlyDepartmentService.verifyOnly(monthlyDepartmentDto)){
				result.error500("部门信息重复，请修改！");
				return result;
			}
			monthlyDepartmentEntity.setDepartName(departName);
			boolean ok = monthlyDepartmentService.updateById(monthlyDepartmentEntity);
			//TODO 返回false说明什么？
			if(ok) {
				result.success("更新成功");
				return result;
			}else {
				return result.error500("更新失败");
			}
		}
	}
	
	/**
	 * @param jsonObject
	 * @return
	 */
	@AutoLog(value = "月卡车部门组织表-通过id删除")
	@ApiOperation(value="月卡车部门组织表-通过id删除", notes="月卡车部门组织表-通过id删除")
	@PostMapping(value = "/delete")
	public Result delete(@RequestBody JSONObject jsonObject) {
		String id=jsonObject.getString("id");
		return monthlyDepartmentService.deleteDepartment(id);
	}

}
